# Create working dir
rm -rf working_dir
mkdir -p working_dir

execute_for_size ()
{
  size=$1
  train_data=$2
  test_data=$3
  prefix=$4
  
  # Take the first size lines from D_train.dat
  head -n $size $train_data > working_dir/train.dat
  for l in -100 -14 0; do
    outfile="${prefix}_out_size_${size}_lambda_${l}.png"
    python train_and_plot.py working_dir/train.dat working_dir/$outfile \
    D_true.dat $l
  done 

  # Cross validation
  head -n $size $train_data > working_dir/train.dat
  rm -rf working_dir/train_*
  num_lines=$((size/5))
  split -l $num_lines working_dir/train.dat working_dir/train_
  outfile="${prefix}_cv_size_${size}"
  python train_and_cross_validate.py working_dir/train_ working_dir/train.dat \
  $test_data D_true.dat working_dir/$outfile $size working_dir/params.dat | \
  awk "{printf(\"%d %s\n\", $size, \$0);}" >> working_dir/${prefix}_size_analysis.dat 
}

for size in 20 40 80 160 200; do
  execute_for_size "$size" "D_train.dat" "D_test.dat" "with_noise"
done

for size in 20 40 80 160 200; do
  execute_for_size "$size" "D_nf_train.dat" "D_nf_test.dat" "without_noise"
done

sed -e 's/PREFIX/with_noise/g' plot_size_data.plt | gnuplot
sed -e 's/PREFIX/without_noise/g' plot_size_data.plt | gnuplot
